package com.gemptc.daoimp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import com.gemptc.bean.Photo_Library;
import com.gemptc.bean.Travelsbean;
import com.gemptc.dao.IInsert;
import com.gemptc.utils.DBUtil;
import com.gemptc.utils.MD5Tool;

public class InsertImp implements IInsert {

	@Override
	public Travelsbean addTravels(Travelsbean travalsbean) {

		Connection connection=DBUtil.getConnection();
		PreparedStatement preparedStatement;
		String sql="insert into travels(user_id, t_title, t_time, t_content, t_hidden) "
				+ "values (?,?,?,?,?)";
		
		try {
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setInt(1, travalsbean.getUser_id());
			preparedStatement.setString(2, travalsbean.getT_tittle());
			preparedStatement.setString(3, travalsbean.getT_time());
			preparedStatement.setString(4, travalsbean.getT_content());
			preparedStatement.setBoolean(5, travalsbean.isT_hidden());
			preparedStatement.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return travalsbean;
	}

	@Override
	public boolean addPhoto_Library(List<Photo_Library> pl_path) {
		boolean isSuccess = false;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		String sql="INSERT INTO photo_library (user_id,t_id,pl_path) VALUES(?,?,?)";
		try {
			connection = DBUtil.getConnection();
			for (int i = 0; i < pl_path.size(); i++) {
				preparedStatement = connection.prepareStatement(sql);
				preparedStatement.setInt(1, pl_path.get(i).getUser_id());
				preparedStatement.setInt(2, pl_path.get(i).getT_id());
				preparedStatement.setString(3, pl_path.get(i).getPl_path());
				preparedStatement.execute();
			}
			isSuccess = true;
		} catch (Exception e) {
			e.printStackTrace();
			isSuccess = false;
		}
		DBUtil.release(null, preparedStatement, connection);
		return isSuccess;
	}

	@Override
	public boolean insert(String user_name, String user_nickname,
			String user_password, String createtime) {
		boolean falg=false;
		Connection connection=DBUtil.getConnection();
		PreparedStatement preparedStatement=null;
		String sql = "insert into users(user_name,user_password,user_nickname,user_createTime) values (?,?,?,?)";
		try {
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setString(1, user_name);
			preparedStatement.setString(2, MD5Tool.MD5(user_password));
			preparedStatement.setString(3, user_nickname);
			preparedStatement.setString(4, createtime);;
			preparedStatement.execute();
			falg=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return falg;

	}

}
